package com.kaifamiao.www.demo.do0902;

import java.sql.*;

public class Query {
    public static void main(String[] args) {
        //1 建立驱动连接
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2 连接数据库
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kfm?user=root&password=");
            //建立SQL对象
            Statement statement = connection.createStatement();
            //定义SQL语句
            String sql = "select id,username,password,createDate from users";
            ResultSet resultSet = statement.executeQuery(sql);

            if (resultSet!=null){
                while (resultSet.next()){
                    //resultSet 代表一行数据
                    int id = resultSet.getInt(1);
                    String username = resultSet.getString(2);
                    String password = resultSet.getString("password");
                    Timestamp date =  resultSet.getTimestamp("createdate");
                    System.out.println(id+"\t" + username + "\t" + password + "\t" +date);
                }
            }

            //关闭连接
            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
